package com.mll.snake.component;

import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class RemoteComponent extends Component {
    Paint backPaint = new Paint();//背景画笔
    Paint bubblePaint = new Paint();//气泡画笔
    Paint rectfPaint = new Paint();
    /**
     * 气泡的位置
     */
    float bubbleX = 300, bubbleY = 300;

    /**
     * 背景圆的位置
     */
    float backX = 300, backY = 300;
    /**
     * 气泡和背景的半径
     */
    int radiusBack = 200, radiusBubble = 100;

    RectFloat mRectF = new RectFloat(backX-radiusBack,backY-radiusBack,backX+radiusBack,backY+radiusBack);


    Context mContext;

    /**
     * STOP  停止
     * RETURN 后退
     * LEFT 左转
     * RIGHT 右转
     * GO 前进
     * 默认为停止
     */
    String orientation="STOP";

    private DrawTask task = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            initPaint();
            canvas.drawCircle(backX, backY, radiusBack, backPaint);

            if (orientation=="GO") {
                Arc arc = new Arc();
                arc.setArc(045,-90,true);
                canvas.drawArc(mRectF, arc, rectfPaint);
            }else if (orientation=="RETURN"){
                Arc arc = new Arc();
                arc.setArc(45,90,true);
                canvas.drawArc(mRectF, arc, rectfPaint);
            }else if (orientation=="LEFT"){
                Arc arc = new Arc();
                arc.setArc(135,90,true);
                canvas.drawArc(mRectF, arc, rectfPaint);
            }else if (orientation=="RIGHT"){
                Arc arc = new Arc();
                arc.setArc(-45,90,true);
                canvas.drawArc(mRectF,arc, rectfPaint);

            }else if (orientation=="STOP"){
                rectfPaint.setAlpha(0);
                Arc arc = new Arc();
                arc.setArc(-90,360,true);
                canvas.drawArc(mRectF, arc, rectfPaint);
            }

            canvas.drawCircle(bubbleX, bubbleY, radiusBubble, bubblePaint);
        }

    };

    private void initPaint() {
        backPaint.setAntiAlias(true);
        backPaint.setColor(Color.RED);

        bubblePaint.setAntiAlias(true);
        bubblePaint.setColor(Color.BLACK);

        rectfPaint.setAntiAlias(true);
        rectfPaint.setColor(Color.WHITE);
        rectfPaint.setAlpha(144);
    }


    /**
     * 得到手指触控点与圆点中心的距离
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
    private float getDistance(float x1, float y1, float x2, float y2) {
        float dis;
        dis = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        return dis;
    }

    /**
     * 当手指触控点在大圆外面时
     * 需要重新得到气泡的位置
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param dis
     * @return
     */
    private float[] getXY(float x1, float y1, float x2, float y2, float dis) {
        float[] xAndy = new float[2];
        float scaleDis;
        float xDis;
        float yDis;


        /**
         * 表示在第一象限之内
         */
        if (x1 > x2 && y1 < y2) {
            scaleDis = radiusBack / dis;
            xDis = Math.abs(x1 - x2);
            yDis = Math.abs(y1 - y2);
            xAndy[0] = x2 + xDis * scaleDis;
            xAndy[1] = y2 - yDis * scaleDis;

        }
        /**
         * 表示在第二象限之内
         */
        else if (x1 < x2 && y1 < y2) {
            scaleDis = radiusBack / dis;
            xDis = Math.abs(x1 - x2);
            yDis = Math.abs(y1 - y2);
            xAndy[0] = x2 - xDis * scaleDis;
            xAndy[1] = y2 - yDis * scaleDis;
        }
        /**
         *表示在第三象限之内
         */
        else if (x1 < x2 && y1 > y2) {
            scaleDis = radiusBack / dis;
            xDis = Math.abs(x1 - x2);
            yDis = Math.abs(y1 - y2);
            xAndy[0] = x2 - xDis * scaleDis;
            xAndy[1] = y2 + yDis * scaleDis;
        }

        /**
         * 表示在第四象限之内
         */
        else if (x1 > x2 && y1 > y2) {
            scaleDis = radiusBack / dis;
            xDis = Math.abs(x1 - x2);
            yDis = Math.abs(y1 - y2);
            xAndy[0] = x2 + xDis * scaleDis;
            xAndy[1] = y2 + yDis * scaleDis;
        }

        /**
         * 角度为零度
         */
        else if (x1 > x2 && y1 == y2) {
            xAndy[0] = x2 + radiusBack;
            xAndy[1] = y2;
        }

        /**
         * 角度为90度
         */
        else if (x1 == x2 && y1 < y2) {
            xAndy[0] = x2;
            xAndy[1] = y2 - radiusBack;
        }

        /**
         * 角度为180度
         */
        else if (x1 < x2 && y1 == y2) {
            xAndy[0] = x2 - radiusBack;
            xAndy[1] = y2;
        }

        /**
         * 表示为270度
         */
        else if (x1 == x2 && y1 > y2) {
            xAndy[0] = x2;
            xAndy[1] = y2 + radiusBack;
        }
        return xAndy;
    }
    /**
     * 更具摇杆操作的方向来控制小车的运动方向
     */
    private void getOrientation(float x,float y){
        if (y<backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){
            orientation = "GO";
        }else if (x>backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){
            orientation="RIGHT";
        }else if (y>backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){
            orientation="RETURN";
        }else if (x<backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){
            orientation="LEFT";
        }else {
            orientation="STOP";
        }

    }

    public RemoteComponent(Context context) {
        super(context);
        this.mContext = context;
        addDrawTask(task);
        this.setTouchEventListener(new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                MmiPoint point = touchEvent.getPointerScreenPosition(0);
                switch (touchEvent.getAction()) {
                    case TouchEvent.POINT_MOVE:
                        float x = point.getX();
                        float y = point.getY();
                        if (getDistance(x, y, backX, backY) < radiusBack) {
                            bubbleX = x;
                            bubbleY = y;
                        } else if (getDistance(x, y, backX, backY) >= radiusBack) {
                            float xAndy[];
                            xAndy = getXY(x, y, backX, backY, getDistance(x, y, backX, backY));
                            bubbleX = xAndy[0];
                            bubbleY = xAndy[1];
                            getOrientation(x,y);
                        }
                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        bubbleX = backX;
                        bubbleY = backY;
                        orientation="STOP";
                        break;
                    default:
                        break;
                }
                invalidate();
                return true;
            }
        });
    }
}
